package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.system.domain.BudgetAccount;
import com.ruoyi.system.domain.BudgetDto;
import com.ruoyi.system.mapper.BudgetMapper;
import com.ruoyi.system.service.BudgetService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, BudgetAccount> implements BudgetService {
    @Autowired
    private BudgetMapper mapper;
    @Autowired
    private Validator validator;
    @Autowired
    private ISysConfigService configService;
    @Override
    public List<BudgetDto> selectBlist(BudgetDto budgetDto) {
        return mapper.selectBlist(budgetDto);
    }

    @Override
    public String importBud(List<BudgetAccount> budList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(budList) || budList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BudgetAccount budgetAccount : budList)
        {
            try
            {
                // 验证是否存在这个用户
                LambdaQueryWrapper<BudgetAccount> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BudgetAccount::getBudgetName, budgetAccount.getBudgetName());
                BudgetAccount selectOne = mapper.selectOne(queryWrapper);
                if (StringUtils.isNull(selectOne))
                {
                    BeanValidators.validateWithException(validator, budgetAccount);
                    budgetAccount.setCreateBy(operName);
                    mapper.insert(budgetAccount);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + budgetAccount.getBudgetName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, budgetAccount);
                    budgetAccount.setUpdateBy(operName);
                    mapper.updateById(budgetAccount);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + budgetAccount.getBudgetName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + budgetAccount.getBudgetName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + budgetAccount.getBudgetName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
